package me.moscrop.RootCommand.utilities;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.plugin.Plugin;

public class MV {
	
	public static boolean addworld(String worldName, Environment worldEvn, String worldSeed, String WorldGenerator) {	
		World world = null; 	
		long seed = 0;
		
		if (worldSeed != null && worldSeed.length() > 0) {
            try {
                seed = Long.parseLong(worldSeed);
            } catch (NumberFormatException numberformatexception) {
                seed = (long) worldSeed.hashCode();
            }
        }
				   
	        if (worldSeed != null) {
	            if (WorldGenerator != null) {
	            	world = WorldCreator.name(worldName).seed(seed).environment(worldEvn).generator(WorldGenerator).createWorld();
	            } else {
	            	world = WorldCreator.name(worldName).seed(seed).environment(worldEvn).createWorld();
	            	
	            }
	        } else {
	            if (WorldGenerator != null) {
	                world = WorldCreator.name(worldName).environment(worldEvn).generator(WorldGenerator).createWorld();
	            } else {
	                world = WorldCreator.name(worldName).environment(worldEvn).createWorld();
	            }
	        }
		
		return (world != null)  ? true : false;
	}
	
	public static void setSpawn(Player player) {
		Location spawn = player.getLocation();
		player.getWorld().setSpawnLocation(spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ());
	}
	
	public static ChunkGenerator getChunkGenerator(String generator, String generatorID, String worldName) {
        if (generator == null) {
            return null;
        }

        Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin(generator);
        if (plugin == null) {
            return null;
        } else {
            return plugin.getDefaultWorldGenerator(worldName, generatorID);

        }
    }
		
	public static Environment getEnvFromString(String evn){
		for(Environment environment : Environment.values()){
			if(evn.equalsIgnoreCase(environment.toString())){
				return environment;
			}
		}
		return Environment.NORMAL;
	}
	
}
